/**
 * Created by Administrator on 2016/10/16.
 */
angular.module('resetPassword.controller', [])
    .controller('resetPasswordByEmailCtrl', ['$resetPasswordService', '$stateParams', '$scope','$location',
        function ($resetPasswordService, $stateParams, $scope,$location) {
            $scope.submitBtnStr = "发送邮件";
            $scope.submitClickable = true;
            $scope.wait = 60;

            $scope.user = {};
            /*
             user.email
             user.password
             */
            $scope.checkResult = {
                isValid:false,
                validMessage : '',
                validType : ''
            }

            $scope.sendEmail = function () {
                if(!$scope.submitClickable){
                    return ;
                }

                $scope.registerSuccess = false;
                $scope.checkEmail();

                if($scope.checkResult.isValid){

                    var params = {email:$scope.user.email};
                    $resetPasswordService.sendEmail(params,function(data){
                        if(data && data.data){
                            var resultData = data.data;
                            if(resultData.result){
                                $scope.downCount();
                            }
                            else {
                                $scope.checkResult = {
                                    isValid:false,
                                    validType:'submit',
                                    validMessage : resultData.message
                                }
                            }

                        }else{
                            $scope.checkResult = {
                                isValid:false,
                                validType:'submit',
                                validMessage : '请求失败,网络异常'
                            }
                        }

                    },function(err){
                        $scope.checkResult = {
                            isValid:false,
                            validType:'submit',
                            validMessage : '网络异常'
                        }
                    });

                }
            }

            $scope.checkEmail = function () {
                $scope.checkResult = {
                    isValid:false,
                    validMessage : '',
                    validType : ''
                }

                var isValid = verifyEmail($scope.user.email);
                if(!isValid){
                    $scope.checkResult.isValid = isValid;
                    $scope.checkResult.validMessage="邮箱格式不正确";
                    $scope.checkResult.validType = 'email';

                    return ;
                }

                $scope.checkResult.isValid = true;

            };

            $scope.downCount = function () {
                if ($scope.wait == 0) {
                    $scope.submitClickable = true;
                    $scope.submitBtnStr = "发送邮件";
                    $scope.wait = 60;

                }
                else {
                    $scope.submitClickable = false;
                    $scope.wait = $scope.wait - 1;
                    $scope.submitBtnStr = "邮件已发送,请查收(" + $scope.wait + ")";

                    setTimeout(function () {
                        $scope.$apply($scope.downCount())
                    }, 1000);
                }
            };

        }])
    .controller('setNewPasswordByEmailCtrl', ['$resetPasswordService', '$stateParams', '$scope','$state','$location',
        function ($resetPasswordService, $stateParams, $scope, $state,$location) {
            $scope.params = $location.search();
            if(!$scope.params || !$scope.params.email || !$scope.params.authToken){
                $scope.paramsError = true;
                return ;
            }
            $scope.paramsError = false;

            $scope.user = {};
            /*
             user.password
             user.confirmPassword
             */
            $scope.checkResult = {
                isValid: false,
                validMessage: '',
                validType:''
            }
            $scope.registerSuccess = false;

            $scope.updatePassword = function () {
                if($scope.paramsError){
                    return ;
                }

                $scope.checkPassword();

                if ($scope.checkResult.isValid) {
                    var params = {
                        email: $scope.params.email,
                        password: $scope.user.password,
                        authToken: $scope.params.authToken
                    }

                    $resetPasswordService.updatePasswordByEmail(params,function(data){
                        if(data && data.data){
                            var resultData = data.data;

                            if(resultData.result){
                                $scope.registerSuccess = true;
                                setTimeout(function(){
                                    $state.go('login');
                                },1000);
                            }
                            else {
                                $scope.checkResult.isValid = false;
                                $scope.checkResult.validMessage=resultData.message;
                                $scope.checkResult.validType='submit';
                            }

                        }else {
                            $scope.checkResult.isValid = false;
                            $scope.checkResult.validMessage="请求超时";
                            $scope.checkResult.validType='submit';
                        }
                    },function(error){
                        $scope.checkResult.isValid = false;
                        $scope.checkResult.validMessage="重置密码失败,请求超时";
                        $scope.checkResult.validType='submit';
                    })

                }
            }

            $scope.checkPassword = function () {
                $scope.checkResult = {
                    isValid: false,
                    validMessage: '',
                    validType:''
                }

                if (!$scope.user.password) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "请输入密码";
                    $scope.checkResult.validType = 'password';
                    return;
                }

                if ($scope.user.password.length  < 6) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "密码长度小于6";
                    $scope.checkResult.validType = 'password';
                    return;
                }

                if ($scope.user.password != $scope.user.confirmPassword) {
                    $scope.checkResult.isValid = false;
                    $scope.checkResult.validMessage = "密码输入不一致";
                    $scope.checkResult.validType = 'confirmPassword';
                    return;
                }

                $scope.checkResult.isValid = true;

            }

        }])
